/**   
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 23:14:00  
 * Copyright (c) 2015- -版权所有     
 */

package com.joysuch.wwyt.edu.controller;

import java.util.List;

import javax.validation.Valid;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import com.joysuch.wwyt.edu.bean.EduQuestionEditBean;
import com.joysuch.wwyt.edu.bean.EduQuestionListBean;
import com.joysuch.wwyt.edu.bean.EduQuestionListBean.SearchEduQuestion;
import com.joysuch.wwyt.edu.entity.EduQuestion;
import com.joysuch.wwyt.edu.repository.EduTestPaperQuestionDao;
import com.joysuch.wwyt.edu.service.EduQuestionService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "教育-题库管理")
@RequestMapping("/edu/question")
public class EduQuestionController {

    private static final Logger logger = LoggerFactory.getLogger(EduQuestionController.class);

    @Autowired
    private EduQuestionService eduQuestionService;
    @Autowired
    private SimpleSelectListService simpleSelectListService;
    @Autowired
    private EduTestPaperQuestionDao eduTestPaperQuestionDao;

    @RequiresPermissions("edu:question:add")
    @RequestMapping("add")
    public Object add(@Valid @RequestBody EduQuestionEditBean entity) {
        eduQuestionService.add(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("edu:question:update")
    @RequestMapping("update")
    public Object update(@Valid @RequestBody EduQuestionEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        eduQuestionService.update(entity);
        return ResultBean.success("");
    }

    // @RequiresPermissions("edu:question")
    @RequestMapping("page")
    public Object page(Pageable page, SearchEduQuestion condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<EduQuestionListBean> data = eduQuestionService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    // @RequiresPermissions("edu:question")
    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchEduQuestion condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<EduQuestionListBean> data = eduQuestionService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequestMapping("simple-list")
    public Object simpleList() {
        List<SimpleSelectListBean> data = simpleSelectListService.list(EduQuestion.class, "name");
        return ResultBean.success(data);
    }

    @RequiresPermissions("edu:question:delete")
    @RequestMapping("delete")
    public Object delete(Long id) throws Exception {
        int count = eduTestPaperQuestionDao.countByQuestionId(id);
        if (count > 0) {
            throw new IllegalArgumentException("该试题已成为试卷试题，请勿删除");
        }
        eduQuestionService.delete(id);
        return ResultBean.success("");
    }

    // @RequiresPermissions("edu:question")
    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        return ResultBean.success(eduQuestionService.getById(id));
    }
}
